#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Bootloader_RequestDownload_0x34_test.py
@Time    :   2024/09/11 10:35:23
@Author  :   yangliang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from numpy import byte, bytes_, int16
from pytest_assume.plugin import assume

import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

SID_0x34_RequestDownload_NRC = [   ([0x34], [0x7F, 0x34, 0x13]),
                                   ([0x34, 0x00], [0x7F, 0x34, 0x13]),
                                   ([0x34, 0x00, 0x44], [0x7F, 0x34, 0x13]),
                                   ([0x34, 0x00, 0x44, 0x00, 0x05, 0x00, 0x00,0x00, 0x00, 0x00], [0x7F, 0x34, 0x13]),
                                   ([0x34, 0x00, 0x33, 0x05, 0x00, 0x00, 0x00,0x00, 0x00, 0x00], [0x7F, 0x34, 0x13]),
                                   ([0x34, 0x10, 0x44, 0x00, 0x03, 0x00, 0x00,0x00, 0x00, 0x00], [0x7F, 0x34, 0x13]),
                                   ([0x34, 0x10, 0x44, 0x00, 0x03, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,0x00], [0x7F, 0x34, 0x13]),
                                   ([0x34, 0x01, 0x44, 0x00, 0x05, 0x00, 0x00,0x00, 0x00, 0x00, 0x01], [0x7F, 0x34, 0x31]),
                                ]
''' NRC31
requestOutOfRange(请求超过限值)
ROOR
下述情况下，应返回此 NRC:
特定 dataFormatldentifier(数据格式标识符)无效。
特定 addressAndLengthFormatldentifier(地址和长度格式标识符)无效。
特定 memoryAddress/memorySize(内存地址/内存大小)无效。
'''
SID_0x34_RequestDownload_NRC31 = [ ('34 00 44 00 05 00 00 FF FF FF FF','7F 34 70'), # APP地址正确，长度错误
                                   ('34 00 44 00 7A 00 00 FF FF FF FF','7F 34 70'), # flashdrv地址正确，长度错误
                                   ('34 00 44 00 02 00 00 FF FF FF FF','7F 34 70'), # pro_boot地址正确，长度错误
                                   ('34 00 44 00 02 A0 00 FF FF FF FF','7F 34 70'), # plus_boot地址 正确，长度错误 
                                   ('34 00 44 FF FF FF FF 00 09 7F 22','7F 34 70'), # 地址错误
                                   ('34 00 44 FF FF FF FF FF FF FF FF','7F 34 70'), # 地址长度都错误
                                   ('34 00 55 00 05 00 00 00 09 7F 22','7F 34 31'), # 
                                   ('34 11 44 00 05 00 00 00 09 7F 22','7F 34 31'),
                                   ('34 10 44 00 05 00 00 00 09 7F 22','7F 34 31'),
                                   ('34 01 44 00 05 00 00 00 09 7F 22','7F 34 31'),          
                                 ] 

class Test_RequestDownload_Phy(object):

    ids = ["Request {2}".format(req, resp, binascii.b2a_hex(bytearray(req), ' ')) for req, resp in SID_0x34_RequestDownload_NRC]
    @allure.severity('minor')
    @allure.description('Test NRC of 0x31 in bootloader with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    @pytest.mark.parametrize('req,resp', SID_0x34_RequestDownload_NRC, ids=ids)
    def test_RequestDownload_NRC_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)

    # 只有bootloader软件时，才执行此测试用例
    @allure.severity('minor')
    @allure.description('Test NRC33 of 0x34 in programming session in bootloader with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_RequestDownload_NRC33_Phy(self,Driver,action):
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x34,0x00,0x44,0x00,0x7A,0x00,0x00,0x00,0x00,0x28,0x0A], [0x7F,0x34,0x33], 2, iStep, False)

    @allure.severity('minor')
    @allure.description('Test NRC7F of 0x34 in default session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_RequestDownload_NRC7F_Default_Phy(self,Driver,action):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x34,0x00,0x44,0x00,0x7A,0x00,0x00,0x00,0x00,0x28,0x0A], [0x7F,0x34,0x7F], 1, iStep, False)

    @allure.severity('minor')
    @allure.description('Test NRC7F of 0x34 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_RequestDownload_NRC7F_Extended_Phy(self,Driver,action):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x34,0x00,0x44,0x00,0x7A,0x00,0x00,0x00,0x00,0x28,0x0A], [0x7F,0x34,0x7F], 3, iStep, False)

    ids = ["Request {0}".format(req,resp) for req,resp in SID_0x34_RequestDownload_NRC31]
    @allure.severity('minor')
    @allure.description('Test NRC31 of 0x31 in bootloader with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    @pytest.mark.parametrize('req,resp',SID_0x34_RequestDownload_NRC31,ids = ids)
    def test_RequestDownload_NRC31_Phy(self,action,Driver,req,resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)

